# -*- coding: utf-8 -*-
# @Time    : 2020/12/11 0:08
# @Author  : Guo_Zhanyu
# @File    : GomokuPyQt5.py
# @Comment : 可单机可联机，运行该文件为单机版
import sys
import socket
import BoardUIDependencies
from PyQt5.QtCore import Qt, QCoreApplication
from PyQt5.QtGui import QPainter, QColor, QPen, QFont, QPalette, QBrush
from PyQt5.QtWidgets import QApplication, QMainWindow
from functools import partial


# 棋盘窗口绘制
"""Globals"""
SIZE = 30  # 棋盘每个点的距离
LINES = 19  # 棋盘的行/列数
OUTER_WIDTH = 20  # 棋盘外宽度
INSIDE_WIDTH = 4  # 边框与棋盘最外的小间隔
BORDER_WIDTH = 4  # 边框宽度
BORDER_LENGTH = SIZE*(LINES - 1) + INSIDE_WIDTH*2 + BORDER_WIDTH  # 边框线长度
START_X = START_Y = OUTER_WIDTH + int(BORDER_WIDTH/2) + INSIDE_WIDTH  # 边框左上角坐标
SCREEN_HEIGHT = SIZE*(LINES - 1) + OUTER_WIDTH*2 + BORDER_WIDTH + INSIDE_WIDTH*2  # 屏幕高度
SCREEN_WIDTH = SCREEN_HEIGHT + 200  # 游戏屏幕的宽

# 棋子绘制
STONE_R = SIZE//2 - 3  # 棋子半径
STONE_R2 = SIZE//2 + 3

# 颜色
BOARD_COLOR = QColor(0xFA, 0xBE, 0x6E)  # 背景颜色
BLACK_COLOR = QColor(0, 0, 0)  # 黑色
WHITE_COLOR = QColor(255, 255, 255)  # 白色
RED_COLOR = QColor(200, 30, 30)  # 红色
BLUE_COLOR = QColor(30, 30, 200)  # 蓝色
BLACK_STONE_COLOR = QColor(45, 45, 45)  # 黑子颜色
WHITE_STONE_COLOR = QColor(219, 219, 219)  # 白子颜色

# 偏移量，用以遍历棋盘
offset = [(1, 0), (0, 1), (1, 1), (1, -1)]


# black or white，value = 1为黑，2为白
class Chessman:
    def __init__(self, name, value, color):
        self.name = name  # black / white
        self.value = value  # 1 / 2
        self.color = color  # (45, 45, 45) / (219, 219, 219)
        pass
    pass


BLACK_CHESSMAN = Chessman('black', 1, BLACK_STONE_COLOR)
WHITE_CHESSMAN = Chessman('white', 2, WHITE_STONE_COLOR)


# 游戏坐标系，左上角为(0，0)，最大为(LINES - 1, LINES - 1)
# 竖直向下为Y轴方向，水平向右为X轴方向
class Point:
    def __init__(self, x=0, y=0):
        self.x = x
        self.y = y
        pass
    pass


class BoardUI(QMainWindow):
    def __init__(self, lines):
        super(BoardUI, self).__init__()
        # 使用designer的UI
        self.uiDependence = BoardUIDependencies.Ui_MainWindow()
        self.uiDependence.setupUi(self)

        # 重载
        self.setWindowTitle('Gomoku')
        self.resize(SCREEN_WIDTH, SCREEN_HEIGHT)

        # 背景颜色设定
        palette1 = QPalette()
        palette1.setColor(self.backgroundRole(), BOARD_COLOR)
        self.setPalette(palette1)

        self._lines = lines
        self._board = None
        self.cur_runner = None
        self.winner = None
        self.last = None
        # 重置棋盘信息
        self.initBoard()

        # 服务器地址与端口号（默认值），后续可以输入
        self.url = 'http://127.0.0.1:5000/'

        self.uiDependence.lineEdit_2.setText(self.getLocalIP())
        self.uiDependence.pushButton.clicked.connect(partial(self.buttonFunc, self.uiDependence))
        self.uiDependence.pushButton_4.clicked.connect(self.regret)

        # 显示
        self.show()
        pass

    def initBoard(self):
        self._board = [[0] * self._lines for _ in range(self._lines)]  # 矩阵[LINESxLINES]
        self.cur_runner = BLACK_CHESSMAN
        self.winner = None
        self.last = None
        pass

    def paintEvent(self, event):
        qp = QPainter(self)
        qp.begin(self)
        # 抗锯齿
        qp.setRenderHint(QPainter.Antialiasing)
        self.drawBoard(event, qp)  # 画棋盘
        self.drawStones(event, qp)  # 画棋子
        if self.winner is not None:
            self.drawWinText(event, qp)  # 画结算信息，=>弹窗？
            pass
        qp.end()
        pass

    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            if self.winner is None:
                mouse_pos = [event.pos().x(), event.pos().y()]
                # print(mouse_pos)
                click_point = self.getClickPoint(mouse_pos)
                if click_point is not None:
                    if self.canDrop(click_point):
                        self.winner = self.drop(self.cur_runner, click_point)
                        self.last = click_point
                        self.cur_runner = self.getNextRunner(self.cur_runner)
                        pass
                    pass
                else:
                    print('超过棋盘范围...')
                    pass
                self.update()
                pass
            pass
        elif event.button() == Qt.RightButton:
            if self.winner is not None:
                self.initBoard()
                self.update()
                pass
            pass
        pass

    def getClickPoint(self, click_pos):
        pos_x = click_pos[0] - START_X
        pos_y = click_pos[1] - START_Y
        if pos_x < -INSIDE_WIDTH or pos_y < -INSIDE_WIDTH:
            return None
        x = pos_x // SIZE
        y = pos_y // SIZE
        if pos_x % SIZE > SIZE / 2:
            x += 1
            pass
        if pos_y % SIZE > SIZE / 2:
            y += 1
            pass
        if x >= LINES or y >= LINES:
            return None

        return Point(x, y)

    def getNextRunner(self, cur_runner):
        if cur_runner == BLACK_CHESSMAN:
            return WHITE_CHESSMAN
        else:
            return BLACK_CHESSMAN
        pass

    def drawBoard(self, event, qp):
        pen = QPen(BLACK_COLOR, BORDER_WIDTH, Qt.SolidLine)
        qp.setPen(pen)
        qp.drawRect(OUTER_WIDTH, OUTER_WIDTH, BORDER_LENGTH, BORDER_LENGTH)
        self.drawLinesHorizontally(event, qp)
        self.drawLinesVertically(event, qp)
        self.drawBoardPoints(event, qp)
        pass

    def drawLinesHorizontally(self, event, qp):
        pen = QPen(BLACK_COLOR, 1, Qt.SolidLine)
        qp.setPen(pen)
        for i in range(LINES):
            qp.drawLine(START_X, START_Y + SIZE*i, START_X + SIZE*(LINES - 1), START_Y + SIZE*i)
            pass
        pass

    def drawLinesVertically(self, event, qp):
        pen = QPen(BLACK_COLOR, 1, Qt.SolidLine)
        qp.setPen(pen)
        for j in range(LINES):
            qp.drawLine(START_X + SIZE*j, START_Y, START_X + SIZE*j, START_Y + SIZE*(LINES - 1))
            pass
        pass

    def drawBoardPoints(self, event, qp):
        qp.setBrush(BLACK_COLOR)
        for i in (3, 9, 15):
            for j in (3, 9, 15):
                if i == j == 9:
                    r = 5
                else:
                    r = 3
                    pass
                qp.drawEllipse(START_X + SIZE*i - r, START_Y + SIZE*j - r, r*2, r*2)
                pass
            pass
        pass

    def drawStones(self, event, qp):
        color = BLACK_STONE_COLOR if self.cur_runner == BLACK_CHESSMAN else WHITE_STONE_COLOR
        pen = QPen(color, 1, Qt.SolidLine)
        brush = QBrush(color)
        qp.setPen(pen)
        qp.setBrush(brush)
        qp.drawEllipse(650, 30, STONE_R2 * 2, STONE_R2 * 2)
        if self.last is not None:
            pen.setColor(RED_COLOR)
            brush = QBrush(Qt.NoBrush)
            qp.setBrush(brush)
            qp.setPen(pen)
            qp.drawRect(START_X + SIZE*self.last.x - STONE_R, START_Y + SIZE*self.last.y - STONE_R, STONE_R*2, STONE_R*2)
        for i, row in enumerate(self.getBoard()):
            for j, cell in enumerate(row):
                if cell == BLACK_CHESSMAN.value:
                    self.drawStone(event, qp, Point(j, i), BLACK_CHESSMAN.color)
                elif cell == WHITE_CHESSMAN.value:
                    self.drawStone(event, qp, Point(j, i), WHITE_CHESSMAN.color)
                    pass
                pass
            pass
        pass

    def drawStone(self, event, qp, point, stone_color):
        pen = QPen(stone_color, 1, Qt.SolidLine)
        qp.setPen(pen)
        qp.setBrush(stone_color)
        qp.drawEllipse(START_X + SIZE*point.x - STONE_R, START_Y + SIZE*point.y - STONE_R, STONE_R*2, STONE_R*2)
        pass

    def drawWinText(self, event, qp):
        pen = QPen(RED_COLOR, 1, Qt.SolidLine)
        font = QFont('SimHei', 72)
        qp.setPen(pen)
        qp.setFont(font)
        qp.drawText(SCREEN_WIDTH // 2 - 220, SCREEN_HEIGHT//2, f"{self.winner.name} win!")
        font = QFont('SimHei', 32)
        qp.setFont(font)
        qp.drawText(SCREEN_WIDTH // 2 - 320, SCREEN_HEIGHT // 2 + 150, "Press RightButton to Continue...")
        pass

    def getBoard(self):
        return self._board

    def setBoard(self, new_board):
        self._board = new_board
        pass

    def canDrop(self, point):
        return self._board[point.y][point.x] == 0

    def drop(self, chessman, point):
        self._board[point.y][point.x] = chessman.value

        # 判断胜利
        if self.win(point):
            print(f'{chessman.name} win, (Press SPACE to continue...)')
            return chessman
        pass

    def win(self, point):
        cur_value = self._board[point.y][point.x]
        for item in offset:
            if self.getCountOnDirection(point, cur_value, item[0], item[1]) >= 5:
                return True
            pass
        pass

    def getCountOnDirection(self, point, value, x_offset, y_offset):
        count = 1
        for step in range(1, 5):
            x = point.x + step*x_offset
            y = point.y + step*y_offset
            if 0 <= x < self._lines and 0 <= y < self._lines and self._board[y][x] == value:
                count += 1
            else:
                break
                pass
            pass

        for step in range(1, 5):
            x = point.x - step*x_offset
            y = point.y - step*y_offset
            if 0 <= x < self._lines and 0 <= y < self._lines and self._board[y][x] == value:
                count += 1
            else:
                break
                pass
            pass

        return count

    def buttonFunc(self, ui):
        dataIn = ui.lineEdit.text()
        if dataIn != '':
            self.url = f'http://{dataIn}:5000/'
            pass
        ui.label_3.setText(self.url)
        pass

    def getLocalIP(self):
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            s.connect(('8.8.8.8', 80))
            ip = s.getsockname()[0]
        finally:
            s.close()
            pass
        return ip

    def regret(self):
        if self.last is not None:
            temp = self.getBoard()
            temp[self.last.y][self.last.x] = 0
            self.setBoard(temp)
            self.cur_runner = self.getNextRunner(self.cur_runner)
            self.last = None
            self.update()
            pass
        pass

    pass


def main():
    app = QApplication(sys.argv)
    board_ui = BoardUI(LINES)
    sys.exit(app.exec_())
    pass


if __name__ == '__main__':
    main()
    pass
